﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ContractorTimesheet.DataModel;

namespace ContractorTimesheet.Models
{
    public class LeaveEditorModel
    {
        public LeaveEditorModel()
        {
            ResetNewLeave();

            LeavesInfo = new List<LeaveInfo>();
        }

        public string NewYear { get; set; }

        public string NewMonth { get; set; }

        public string NewDay { get; set; }

        public string NewNote { get; set; }

        public List<LeaveInfo> LeavesInfo { get; set; }

        public void ResetNewLeave()
        {
            NewYear = string.Empty;

            NewMonth = string.Empty;

            NewDay = string.Empty;

            NewNote = string.Empty;
        }

        public LeaveEditorModel SortByDateDescending()
        {
            LeaveEditorModel model = new LeaveEditorModel();
            model.NewYear = NewYear;
            model.NewMonth = NewMonth;
            model.NewDay = NewDay;
            model.NewNote = NewNote;
            model.LeavesInfo = LeavesInfo.OrderByDescending(h => h.Year).ThenByDescending(h => h.Month).ThenByDescending(h => h.Day).ToList();

            return model;
        }

    }

    public class LeaveInfo
    {
        public LeaveInfo() { }

        public LeaveInfo(Leave leave)
        {
            ResourceName = CreateResourceName(leave.User);
            Id = leave.Id;
            Year = leave.Year;
            Month = leave.Month;
            Day = leave.Day;
            Note = leave.Note;
        }

        public int Id { get; set; }

        public string ResourceName { get; set; }

        public string Year { get; set; }

        public string Month { get; set; }

        public string Day { get; set; }

        public string Note { get; set; }

        public static IList<LeaveInfo> CreateLeavesInfo(IList<Leave> leaves)
        {
            List<LeaveInfo> leavesInfo = new List<LeaveInfo>();

            foreach (Leave leave in leaves)
            {
                leavesInfo.Add(new LeaveInfo(leave));
            }

            return leavesInfo;
        }

        private string CreateResourceName(User user)
        {
            return string.Format("{0}, {1}", user.Lastname, user.Firstname);
        }

    }

}